package com.aimei520.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encryption {

	private String password;

	public Encryption() {
	}

	public Encryption(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getEncryptionPassword() throws NoSuchAlgorithmException {
		StringBuffer sb = new StringBuffer();
		sb.append("jia");
		sb.append(password);
		sb.append("xun");
		if(sb.length()>16) {
			sb.append(sb.substring(10));
		}else {
			sb.append(sb.substring(6));
		}
        // 得到一个信息摘要器
        MessageDigest digest = MessageDigest.getInstance("md5");
        byte[] byteArray = digest.digest(new String(sb).getBytes());
        StringBuffer result = new StringBuffer();
        
        // 把每一个byte 做一个与运算 0xff; ==保证高位不会补位为1
        for (byte b : byteArray) {
            // 与运算
            int number = b & 0xff;// 加盐：
            String str = Integer.toHexString(number);
            if (str.length() == 1) {
            	result.append("0");
            }
            result.append(str);
        }
        return new String(result);
	}
	
}
